package com.recharge.web.socket;

import com.google.gson.Gson;
import com.recharge.common.result.ResultDO;
import com.recharge.domain.vo.MonitorDO;
import com.recharge.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ServerEndpoint(value = "/api/monitor")
@Component
public class WebSocketServer {

    private ExecutorService executorService = null;

    @Autowired
    private MonitorService monitorService;

    @PostConstruct
    public void init() {
        executorService =  new ThreadPoolExecutor(1, 10,
                5,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(512),
                new ThreadPoolExecutor.DiscardPolicy());
        executorService.execute(new Push());
    }
    private static Logger log = LoggerFactory.getLogger(WebSocketServer.class.getName());
    private static final AtomicInteger OnlineCount = new AtomicInteger(0);
    // concurrent包的线程安全Set，用来存放每个客户端对应的Session对象。
    private static CopyOnWriteArraySet<Session> SessionSet = new CopyOnWriteArraySet<Session>();


    class Push implements Runnable {

        public void run() {
            while(!Thread.currentThread().isInterrupted()) {
                try {
                    ResultDO result = monitorService.monitor();
                    Gson gson = new Gson();
                    String jsonData = null;
                    if(!result.isSuccess()) {
                        jsonData = gson.toJson(result.getErrorCode());
                    } else {
                        MonitorDO monior = (MonitorDO) result.getModel(ResultDO.FIRST_MODEL_KEY);
                        jsonData = gson.toJson(monior);
                    }
                    BroadCastInfo(jsonData);
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                    Thread.interrupted();
                }
            }
        }
    }

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session) {
        SessionSet.add(session);
        int cnt = OnlineCount.incrementAndGet(); // 在线数加1
        log.info("有连接加入，当前连接数为："+ cnt);
        SendMessage(session, "连接成功");
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(Session session) {
        SessionSet.remove(session);
        int cnt = OnlineCount.decrementAndGet();
        log.info("有连接关闭，当前连接数为："+ cnt);
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message
     *            客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("来自客户端的消息："+message);
        SendMessage(session, "收到消息，消息内容："+message);

    }

    /**
     * 出现错误
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.info("发生错误：{}，Session ID： {}"+error.getMessage()+":"+session.getId());
        error.printStackTrace();
    }

    /**
     * 发送消息，实践表明，每次浏览器刷新，session会发生变化。
     * @param session
     * @param message
     */
    public static void SendMessage(Session session, String message) {
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            log.info("发送消息出错：{}"+e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 群发消息
     * @param message
     * @throws IOException
     */
    public static void BroadCastInfo(String message) throws IOException {
        for (Session session : SessionSet) {
            if(session.isOpen()){
                SendMessage(session, message);
            }
        }
    }

    /**
     * 指定Session发送消息
     * @param sessionId
     * @param message
     * @throws IOException
     */
    public static void SendMessage(String message,String sessionId) throws IOException {
        Session session = null;
        for (Session s : SessionSet) {
            if(s.getId().equals(sessionId)){
                session = s;
                break;
            }
        }
        if(session!=null){
            SendMessage(session, message);
        }
        else{
            log.info("没有找到你指定ID的会话：{}"+sessionId);
        }
    }
}